package com.kugoweb.shthon2011s.tlwatch.service;

import jp.co.sharp.android.hardware.FlashLight;
import twitter4j.DirectMessage;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.Twitter;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.UserList;
import twitter4j.UserStreamListener;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.kugoweb.shthon2011s.tlwatch.commons.SettingsData;
import com.kugoweb.shthon2011s.tlwatch.commons.TweetUtils;

public class WatchService extends Service {

	private static final String TAG = WatchService.class.getSimpleName();

	private static final String KEY_ONESEG = "これ見ろ:";
	private static final String KEY_LIGHT = "光れ";

	private final Handler mHandler = new Handler();

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
	 */
	@Override
	public int onStartCommand(final Intent intent, final int flags,
			final int startId) {
		Log.d(TAG, "onStartCommand");
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				Log.d(TAG, "run");
				try {
					final SettingsData settingsData = new SettingsData(
							getApplicationContext());
					final Twitter twitter = TweetUtils.getTwitter(settingsData);
					if (twitter == null) {
						stopSelf();
						return;
					}
					final String myName = twitter.getScreenName();
					final TwitterStream stream = new TwitterStreamFactory(
							twitter.getConfiguration()).getInstance();
					stream.addListener(new UserStreamListener() {

						@Override
						public void onException(final Exception arg0) {
						}

						@Override
						public void onTrackLimitationNotice(final int arg0) {
						}

						@Override
						public void onStatus(final Status status) {
							if (myName.equals(status.getInReplyToScreenName())) {
								final String text = status.getText();
								Log.d(TAG, text);
								if (text.contains(KEY_ONESEG)) {
									callOneseg(text);
								} else if (text.contains(KEY_LIGHT)) {
									final FlashLight light = new FlashLight();
									light.setFlashLightOn(
											FlashLight.LIGHT_COLOR_WHITE, 500,
											500);
									try {
										Thread.sleep(2 * 1000);
									} catch (final InterruptedException e) {
									}
									light.setFlashLightOff();
								}
							}
						}

						private void callOneseg(final String text) {
							final int a = text.indexOf(KEY_ONESEG)
									+ KEY_ONESEG.length();
							Log.d(TAG, text.substring(a));
							try {
								final int ch = Integer.parseInt(text
										.substring(a));
								// ワンセグ起動
								final Intent intent = new Intent(
										"jp.co.sharp.android.oneseg.view.action.RUN");
								intent
										.putExtra(
												"jp.co.sharp.android.oneseg.EXTRA_KEY_CHANNEL_NO",
												ch);
								intent
										.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
												| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
								startActivity(intent);
								Log.d(TAG, "Here");
							} catch (final NumberFormatException e) {
								// do nothing
							}
						}

						@Override
						public void onScrubGeo(final long arg0, final long arg1) {
						}

						@Override
						public void onDeletionNotice(
								final StatusDeletionNotice arg0) {
						}

						@Override
						public void onUserProfileUpdate(final User arg0) {
						}

						@Override
						public void onUserListUpdate(final User arg0,
								final UserList arg1) {
						}

						@Override
						public void onUserListUnsubscription(final User arg0,
								final User arg1, final UserList arg2) {
						}

						@Override
						public void onUserListSubscription(final User arg0,
								final User arg1, final UserList arg2) {
						}

						@Override
						public void onUserListMemberDeletion(final User arg0,
								final User arg1, final UserList arg2) {
						}

						@Override
						public void onUserListMemberAddition(final User arg0,
								final User arg1, final UserList arg2) {
						}

						@Override
						public void onUserListDeletion(final User arg0,
								final UserList arg1) {
						}

						@Override
						public void onUserListCreation(final User arg0,
								final UserList arg1) {
						}

						@Override
						public void onUnfavorite(final User arg0,
								final User arg1, final Status arg2) {
						}

						@Override
						public void onUnblock(final User arg0, final User arg1) {
						}

						@Override
						public void onRetweet(final User arg0, final User arg1,
								final Status arg2) {
						}

						@Override
						public void onFriendList(final long[] arg0) {
						}

						@Override
						public void onFollow(final User arg0, final User arg1) {
						}

						@Override
						public void onFavorite(final User arg0,
								final User arg1, final Status arg2) {
						}

						@Override
						public void onDirectMessage(final DirectMessage arg0) {
						}

						@Override
						public void onDeletionNotice(final long arg0,
								final long arg1) {
						}

						@Override
						public void onBlock(final User arg0, final User arg1) {
						}
					});
					stream.user();
				} catch (final Exception e) {
					e.printStackTrace();
				}
			}
		});

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(final Intent intent) {
		return null;
	}

}
